﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using PagedList.Mvc;
using PagedList;
using WineStore.Models;
using System.Data;
using System.IO;
using System.Data.Entity;

namespace WineStore.Controllers
{
    public class LinkWebsiteController : Controller
    {
        //
        // GET: /LinkWebsite/

        public ActionResult ShowLinkWebsite(int? page)
        {
            using (var db = new WineDbContext())
            {
                var links = db.LinkWebsites.OrderByDescending(n => n.LinkWebsiteId);

                int pageSize = 5;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(links.ToPagedList(pageNumber, pageSize));
            }
        }
        // GET: /LinkWebsite/CreateLinkWebsites/
        [HttpGet]
        public ActionResult CreateLinkWebsites()
        {

            return View();
        }

        // POST: /LinkWebsite/CreateLinkWebsites/
        [HttpPost]
        public ActionResult CreateLinkWebsites(LinkWebsite links, HttpPostedFileBase LinkImage)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Luu anh len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/LinkImages"), System.IO.Path.GetFileName(LinkImage.FileName));
                    LinkImage.SaveAs(path);
                    // Luu anh vao database 
                    links.LinkImage = LinkImage.FileName;
                    using (var db = new WineDbContext())
                    {
                        db.Entry(links).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowLinkWebsite", "LinkWebsite");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(links);
        }


        // GET: /LinkWebsite/EditLinkWebsites/
        [HttpGet]
        public ActionResult EditLinkWebsites(int id = 0)
        {
            LinkWebsite links = null;
            using (var db = new WineDbContext())
            {
                links = db.LinkWebsites.Find(id);
                if (links == null)
                {
                    return HttpNotFound();
                }
            }
            return View(links);
        }

        // POST: /LinkWebsite/EditLinkWebsites/
        [HttpPost]
        public ActionResult EditLinkWebsites(LinkWebsite links, HttpPostedFileBase LinkImage)
        {
            try
            {
                // Lay ve duong dan anh cu
                links.LinkImage = (new WineDbContext().LinkWebsites.Find(links.LinkWebsiteId)).LinkImage;
                if (ModelState.IsValidField("LinkWebsiteTitle_VN") && ModelState.IsValidField("LinkWebsiteUrl"))
                {
                    // Neu update anh moi thi thuc hien khoi lenh trong than cau lenh if
                    if (LinkImage != null)
                    {
                        // Xoa anh cu tren server
                        FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/LinkImages/" + links.LinkImage));
                        if (fileInfo != null)
                        {
                            fileInfo.Delete();
                        }
                        // Luu anh moi len server
                        string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/LinkImages"), System.IO.Path.GetFileName(LinkImage.FileName));
                        LinkImage.SaveAs(path);
                        // Luu duong dan anh vao database 
                        links.LinkImage = LinkImage.FileName;
                    }

                    using (var db = new WineDbContext())
                    {
                        db.Entry(links).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("ShowLinkWebsite", "LinkWebsite");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }

            return View(links);
        }

        // GET: /LinkWebsite/DeleteLinkWebsites/
        public ActionResult DeleteLinkWebsites(int id)
        {
            LinkWebsite links = null;
            using (var db = new WineDbContext())
            {
                links = db.LinkWebsites.Find(id);
                if (links == null)
                {
                    return HttpNotFound();
                }
            }
            return View(links);
        }

        // POST: /LinkWebsite/DeleteLinkWebsites/
        [HttpPost, ActionName("DeleteLinkWebsites")]
        public ActionResult DeleteConfirmed(int id)
        {
            LinkWebsite links = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    links = db.LinkWebsites.Find(id);
                    // Xoa anh tren server
                    FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/LinkImages/" + links.LinkImage));
                    if (fileInfo != null)
                    {
                        fileInfo.Delete();
                    }
                    db.Entry(links).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowLinkWebsite", "LinkWebsite");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(links);
        }

    }
}
